# -*- coding: utf-8 -*-

from PyQt5.QtCore import( 
    Qt, 
    QPointF,
    QTimer
)
from PyQt5.QtWidgets import (
    QAction
)

from PyQt5.QtGui import (
    QTransform,
    QColor
)

from qgis.gui import (
    QgsMapMouseEvent, 
    QgsMapToolEmitPoint,
    QgsRubberBand
)

from qgis.core import(
    QgsWkbTypes,
    QgsGeometry,
    QgsApplication,
    QgsProject,
    QgsPointXY
) 

import math

import src.QgsExtrasTools_hli.UtilsTool.QETransformToos as QETransform

class RotateGeometryTool(QgsMapToolEmitPoint):
    def __init__(self, canvas,rubber_band:QgsRubberBand):
        QgsMapToolEmitPoint.__init__(self, canvas)
        self.__canvas = canvas
        self.__crs = canvas.mapSettings().destinationCrs()

        self.__rubber_band = rubber_band
        self.__bf_geometry = rubber_band.asGeometry()
        self.__midpoint = self.__bf_geometry.centroid().asPoint()
        self.__is_move = False
        self.__angle_origin = 0

    def activate(self):
        QgsMapToolEmitPoint.activate(self)
        self.__canvas.setCursor(Qt.CrossCursor)

    def deactivate(self):
        QgsMapToolEmitPoint.deactivate(self)


    def canvasPressEvent(self, event):
        point = self.toMapCoordinates(event.pos())
        if event.button() == Qt.LeftButton:
            geometry = self.__rubber_band.asGeometry()
            self.__is_move = True
            self.__bf_geometry = self.__rubber_band.asGeometry()
            self.__angle_origin = self.calculate_angle(self.__midpoint, point)
        elif event.button() == Qt.RightButton:
            if self.__is_move==True:
                self.__rubber_band.reset(QgsWkbTypes.PolygonGeometry)
                self.__rubber_band.setToGeometry(self.__bf_geometry,self.crs)
                    
                self.__is_move = False
                self.__bf_geometry = None


    def canvasMoveEvent(self, event):
        if self.__is_move==True:
            point = self.toMapCoordinates(event.pos())
            geometry =  QgsGeometry(self.__bf_geometry)
            
            angle = QETransform.CalculateAngle(self.__midpoint, point)
            QETransform.RotationApply(self.__midpoint,angle - self.__angle_origin,geometry)

            self.__rubber_band.reset(QgsWkbTypes.PolygonGeometry)
            self.__rubber_band.setToGeometry(geometry,self.__crs)

    def canvasReleaseEvent(self, event):
        if event.button() == Qt.LeftButton:
            if self.__is_move==True:
                point = self.toMapCoordinates(event.pos())
                geometry = QgsGeometry(self.__bf_geometry)

                angle = QETransform.CalculateAngle(self.__midpoint, point)
                QETransform.RotationApply(self.__midpoint,angle - self.__angle_origin,geometry)
                
                self.__rubber_band.reset(QgsWkbTypes.PolygonGeometry)
                self.__rubber_band.setToGeometry(geometry,self.__crs)
                    
            self.__is_move = False
            self.__bf_geometry = None
            

